import Vue from 'vue'
import VueRouter from 'vue-router'
import home from '../views/home'
import friends from '../views/friends'
import cart from '../views/cart'
import search from '../views/search'
import laylout from '../laylout'
import NoFoundPage from '../views/404'
import NewsList from '../views/news/NewsList'
import NewsInfo from '../views/news/NewsInfo'
import PhotoList from '../views/photos/PhotoList'
import PhotoInfo from '../views/photos/Photoinfo.vue'
import GoodsList from '../views/goods/GoodsList.vue'
import GoodsInfo from '../views/goods/GoodsInfo.vue'
import Login from '../views/login'

Vue.use(VueRouter)

const routes = [
  {
    path:"/",
    component:laylout,
    redirect:'home',
    children:[
      {
        path:'/home',
        component:home,
        meta:{
          title:"首页",
          isShow:true
        }
      },
      {
        path:'/friends',
        component:friends,
        meta:{
          title:"朋友",
          isShow:true
        }
      },
      {
        path:'/cart',
        component:cart,
        meta:{
          title:"购物车",
          isShow:false
        }
      },
      {
        path:'/search',
        component:search,
        meta:{
          title:"搜索",
          isShow:true
        }
      },
      {
        path:'/home/newslist',
        component:NewsList,
        meta:{
          title:"新闻",
          isShow:false
        }
      },
      {
        path:'/home/newsinfo/:id',
        component:NewsInfo,
        props:true,
        meta:{
          title:"新闻列表",
          isShow:false
        }
      },
      {
        path:'/home/photolist',
        component:PhotoList,
        meta:{
          title:"图片列表",
          isShow:false
        }
      },
      {
        path:'/home/photoinfo/:id',
        component:PhotoInfo,
        props:true,
        meta:{
          title:"图片详情",
          isShow:false
        }
      },
      {
        path:'/home/goodslist',
        component:GoodsList,
        meta:{
          title:"商品列表",
          isShow:false
        }
      },
      {
        path:'/home/goodsinfo/:id',
        component:GoodsInfo,
        props:true,
        meta:{
          title:"商品详情",
          isShow:false
        }
      }
    ]
  },
  {
    path:"/login",
    component:Login,
    meta:{
      auth:true
    }
  },
  {
    path:'*',
    component:NoFoundPage
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (!to.meta.auth && !localStorage.token) {
    next('/login')
  } else {
    next()
  }
})

export default router
